package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dto.PetDto;
import com.qf.entity.House;
import com.qf.entity.Pet;
import com.qf.exception.SystemException;
import com.qf.mapper.PetMapper;
import com.qf.result.ResponseResult;
import com.qf.service.HouseService;
import com.qf.service.OwnerService;
import com.qf.service.PetService;
import com.qf.utils.BeanCopyUtils;
import com.qf.vo.PetVo;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.qf.enums.AppHttpCodeEnum.*;

@Service("petService")
public class PetServiceImpl extends ServiceImpl<PetMapper, Pet> implements PetService {

    @Resource
    private OwnerService ownerService;

    @Resource
    private HouseService houseService;

    @Override
    public ResponseResult getPetList(String petName) {
        //如果petName不为空,查询所有
        List<Pet> list = list();
        if (!StringUtils.isEmpty(petName)) {
            LambdaQueryWrapper<Pet> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .like(Pet::getPetName, petName)
                    .or()
                    .like(Pet::getCategory, petName);
            list = list(queryWrapper);
        }
        //根据楼号查询楼栋信息和业主姓名
        List<PetVo> petVos = BeanCopyUtils.copyBeanList(list, PetVo.class);
        petVos.stream()
                .filter(petVo -> houseService.getById(petVo.getHouseId()).getOwnId() != null)
                .map(petVo -> petVo.setHouseNum(houseService.getById(petVo.getHouseId()).getHouseNum()))
                .map(petVo -> petVo.setOwnPhone(ownerService.getById(houseService.getById(petVo.getHouseId()).getOwnId()).getOwnPhone()))
                .collect(Collectors.toList());
        return ResponseResult.okResult(petVos);
    }

    @Override
    public ResponseResult deletePet(Integer id) {
        boolean flag = removeById(id);
        if (!flag) {
            throw new SystemException(DELETE_ERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updatePet(PetDto petDto) {
        Pet pet = BeanCopyUtils.copyBean(petDto, Pet.class);
        //进行条件判断
        House house = houseService.getById(pet.getHouseId());
        if (Objects.isNull(house)) {
            return ResponseResult.errorResult(201, "楼栋不存在");
        }
        if (Objects.isNull(house.getOwnId())) {
            return ResponseResult.errorResult(201, "楼栋不存在业主");
        }
        if (Objects.isNull(getById(pet.getId()))) {
            return ResponseResult.errorResult(201, "更新宠物不存在");
        }
        boolean flag = updateById(pet);
        if (!flag) {
            throw new SystemException(UPDATE_ERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult addPet(PetDto petDto) {
        Pet pet = BeanCopyUtils.copyBean(petDto, Pet.class);
//        if (!Objects.isNull(pet.getId())) {
//            return ResponseResult.errorResult(201, "id无需传值");
//        }
        House house = houseService.getById(pet.getHouseId());
        if (Objects.isNull(house)) {
            return ResponseResult.errorResult(201, "楼栋不存在");
        }
        if (Objects.isNull(house.getOwnId())) {
            return ResponseResult.errorResult(201, "楼栋不存在业主");
        }
        pet.setId(null);
        boolean flag = save(pet);
        if (!flag) {
            throw new SystemException(INSERT_ERROR);
        }
        return ResponseResult.okResult();
    }

}

